#include "node.h"

Node* createLinkList(int n) {
    // Node* n1 = new Node(1);   // head, root
    // Node* n2 = new Node(2);
    // Node* n3 = new Node(3);
    // Node* n4 = new Node(4);
    // n1->next = n2;
    // n2->next = n3;
    // n3->next = n4;  // n4->next nullptr;
    if (n == 1) {
        Node* n1 = new Node(1);
        return n1;
    } else {
        Node* next = new Node(n);
        // Node* cur  = new Node();
        for (size_t i = 0; i < n-1; i++)  // TODO: 
        {
            /* code */
            Node* cur = new Node(n-i, next);  // ????
            // cur->val  = n-i;
            // cur->next = next;
            next = cur;
            // return cur;
        }
        return next;
    }
}

// void deleteLinkList(Node* root) {
    
// }

Node* mergeTwoLists(Node* root1, Node* root2) {
    Node* dummy = new Node();
    Node* p     = dummy;
    while(root1 != nullptr && root2 != nullptr) {
        if (root1->val < root2->val) {
            p->next = root1;
            root1   = root1->next;
        } else {
            p->next = root2;
            root2   = root2->next;
        }
        p = p->next;
    }
    p->next = (root1 == nullptr) ? root2 : root1;
    Node* root = dummy->next;
    delete dummy;
    return root;
}


int main(int argc, char** argv){
    // LinkedList link(20);
    // Node* head = link.createLinkedList(); 
    // link.insertNode();
    Node* n1 = new Node(1);   // head, root
    Node* n2 = new Node(2);
    Node* n3 = new Node(3);
    Node* n4 = new Node(4);
    n1->next = n2;
    n2->next = n3;
    n3->next = n4;  // n4->next nullptr;

    // n1 represnet linked list
    // Node* p;
    // 1;  NULL
    // 2;  nullptr
    // for (p = n1; p != NULL; p = p->next) {
    //     printf("%d\n", p->val);
    // }
    // printf("%p %p %d\n",n1, n2, sizeof(Node));


    // free(n2);
    // free(n1);
    delete n4;    // malloc <-> free   
    delete n3;    // new <-> delete
    delete n2;
    delete n1;
    return 0;
}
